package com.quanshu.exchange.ui.gui.profile;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.ShopInterface;
import com.quanshu.exchange.support.entity.SystemMessageEntity;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.shop.base.view.BaseTitleBar;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 系统消息
 * Created by xiniu003 on 2018/1/9.
 */

public class SysMassageActivity extends BaseActivity {

    @BindView(R.id.sys_massage_guan_rel)
    RelativeLayout sysMassageGuanRel;
    @BindView(R.id.sys_massage_qite_rel)
    RelativeLayout sysMassageQiteRel;
    @BindView(R.id.message_title)
    BaseTitleBar messageTitle;
    @BindView(R.id.sys_massage_guan_image)
    ImageView sysMassageGuanImage;
    @BindView(R.id.sys_massage_qite_image)
    ImageView sysMassageQiteImage;

    private List<SystemMessageEntity> datas;

    private List<SystemMessageEntity> officialMassage = new ArrayList<>();
    private List<SystemMessageEntity> elseMassage = new ArrayList<>();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sys_massage);
        ButterKnife.bind(this);

        getSysMassage();
        initListener();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    private void initListener() {
        ImmersionBar.with(this).titleBar(messageTitle).statusBarColor(R.color.white).statusBarDarkFont(true).init();
        messageTitle.setCenterText("系统消息").setLeftImage(R.drawable.icon_back_black_2_0).setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });
    }

    private void setData(List<SystemMessageEntity> dataList) {
        if (dataList != null && dataList.size() > 0) {
            for (int i = 0; i < dataList.size(); i++) {
                if (datas.get(i).getType_name().equals("官方消息")) {
                    officialMassage.add(dataList.get(i));
                } else {
                    elseMassage.add(dataList.get(i));
                }
            }
        }

        //前往官方消息
        RxViewHelper.getInstance().clicks(sysMassageGuanRel, aVoid -> {
            Intent intent = new Intent(SysMassageActivity.this, OfficialMassageActivity.class);
            intent.putExtra("massage", "官方消息");
            if (officialMassage != null && officialMassage.size() > 0) {
                intent.putExtra("officialMassage", (Serializable) officialMassage);
            }
            startActivity(intent);
        });

        //前往其他消息
        RxViewHelper.getInstance().clicks(sysMassageQiteRel, aVoid -> {
            Intent intent = new Intent(SysMassageActivity.this, OfficialMassageActivity.class);
            intent.putExtra("massage", "其他消息");
            if (elseMassage != null && elseMassage.size() > 0) {
                intent.putExtra("officialMassage", (Serializable) elseMassage);
            }
            startActivity(intent);
        });
    }

    //获取系统消息
    private void getSysMassage() {
        NetClient.getInstance().getShopService(ShopInterface.class)
                .getSysMassage(AppUtils.getSignParams(String.valueOf(currentUser.getId()))
                        , String.valueOf(currentUser.getId()))
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse<List<SystemMessageEntity>>>() {
                    @Override
                    public void onSuccess(BaseResponse<List<SystemMessageEntity>> response) {
                        if (response.getData() != null && response.getData().size() > 0) {

                            datas = response.getData();
                            setData(datas);
                        }
                    }
                });
    }

}
